<?php

namespace Admin\Controller;

class PostController extends AdminController {

    public function index() {

        $postWhere['p.is_del'] = 0;
        $postWhere['p.is_system'] = 0;
        $postWhere['p.post_type'] = array('neq', 10);

        $postM = M()->table('ot_post p')
                ->join('left join ot_users u on p.user_id = u.user_id');

        $field = "p.post_id,p.title,p.post_type,p.topic_id,p.user_id,p.content,p.img_list,p.video_url,p.create_time,u.head_imgurl,u.nick_name,p.is_system";
        $list = $this->lists($postM, $postWhere, 'p.create_time desc', $field);


        $info['post_list'] = [];
        if ($list) {
            foreach ($list as &$val) {
                if ($val['img_list']) {
                    $img_list = explode(',', $val['img_list']);
                    $val['img_url'] = $img_list[0];
                } else {
                    $val['img_url'] = '';
                }
                $val['comment_num'] = M('PostComment')->where(['post_id' => $val['post_id']])->count();
            }
            unset($val);

            $info['post_list'] = $list;
        }

        $this->assign('_list', $list);
        $this->assign('meta_title', '帖子管理');
        $this->display();
    }

    public function info() {
        $id = I('id', 0, 'intval');

        if ($id == 0) {
            $this->error('错误的ID');
        }

        $info = M('Post')->where(['post_id' => $id])->find();

        if (empty($info)) {
            $this->error('找不到帖子数据');
        }

        $info['img_list'] = explode(',', $info['img_list']);


        $postWhere['pc.post_id'] = $id;

        $postM = M()->table('ot_post_comment pc')
                ->join('left join ot_users u on pc.user_id = u.user_id');

        $field = "pc.*,u.head_imgurl,u.nick_name";
        $list = $this->lists($postM, $postWhere, 'pc.create_time desc', $field);

        $this->assign('_list', $list);
        $this->assign('data', $info);
        $this->display();
    }

    public function getInfo() {
        $fid = I('fid', 0, 'intval');

        $list = M('Area')->where(array('fid' => $fid))->select();
        $this->ajaxReturn($list);
    }

    public function changeStatus() {
        $s = I('s', 0, 'intval');

        $id = I('id', 0, 'intval');

        if ($id == 0) {
            $this->error('错误的ID');
        }

        $data['is_del'] = $s;

        $res = M('Post')->where(['post_id' => $id])->save($data);

        if ($res) {
            $this->success('操作成功');
        } else {
            $this->error('操作失败');
        }
    }

    public function del() {
        $id = I('id', 0, 'intval');

        if ($id == 0) {
            $this->error('错误的ID');
        }

        $data['is_del'] = 1;

        $res = M('Post')->where(['post_id' => $id])->save($data);

        if ($res) {
            $this->success('删除成功');
        } else {
            $this->error('删除失败');
        }
    }

    public function salePost() {
        $postWhere['is_del'] = 0;
        $postWhere['is_system'] = 0;
        $postWhere['post_type'] = 10;


//         //省
//        $sheng = I("sheng", -1);
//        if ($sheng != -1) {
//
//            $where['province'] = M('Area')->where(array('id' => $sheng))->getField('name');
//            $this->assign("sheng", $sheng);
//        }
//
//        //市
//        $shi = I("shi", -1);
//        if ($shi != -1) {
//            $where['city'] = M('Area')->where(array('id' => $shi))->getField('name');
//
//            $shi_list = M('Area')->where(array('fid' => $sheng))->select();
//            $this->assign('shi_list', $shi_list);
//            $this->assign("shi", $shi);
//        }
//
//        //区
//        $qu = I("qu", -1);
//        if ($qu != -1) {
//
//            $where['district'] = M('Area')->where(array('id' => $qu))->getField('name');
//
//            $qu_list = M('Area')->where(array('fid' => $shi))->select();
//            $this->assign('qu_list', $qu_list);
//            $this->assign("qu", $qu);
//        }
//
//
//        //状态
//        $status = I("status", -1);
//        if ($status != -1) {
//            $where['d.status'] = $status;
//            $this->assign("status", $status);
//        }
//
//        //是否绝育
//        $is_ster = I("is_ster", -1);
//        if ($is_ster != -1) {
//            $where['is_ster'] = $is_ster;
//            $this->assign("is_ster", $is_ster);
//        }

        $keywords = I('keywords');
        if ($keywords) {

            $map['dog_name'] = array('like', '%' . $keywords . '%');
            $map['card_number'] = $keywords;
            $map['u.nick_name'] = array('like', '%' . $keywords . '%');

            $map['_logic'] = 'or';
            $where['_complex'] = $map;

            $this->assign('keywords', $keywords);
        }

        
        $keywords = I('keywords');
        if ($keywords) {

            $postWhere['title'] = array('like', '%' . $keywords . '%');
            $this->assign('keywords', $keywords);
        }

        $postM = M('Post');

        $field = "*";
        $list = $this->lists($postM, $postWhere, 'create_time desc', $field);


        if ($list) {
            foreach ($list as &$val) {
                if ($val['img_list']) {
                    $img_list = explode(',', $val['img_list']);
                    $val['img_url'] = $img_list[0];
                } else {
                    $val['img_url'] = '';
                }
            }
            unset($val);
        }
//        
//         $sheng_list = M('Area')->where(array('fid' => 0))->select();
//        $this->assign('sheng_list', $sheng_list);

        $this->assign('_list', $list);
        $this->assign('meta_title', '售卖管理');
        $this->display();
    }

    public function systemPost() {
        $postWhere['p.is_del'] = 0;
        $postWhere['p.is_system'] = 1;

        $postM = M()->table('ot_post p')
                ->join('left join ot_users u on p.user_id = u.user_id');

        $field = "p.post_id,p.title,p.post_type,p.topic_id,p.user_id,p.content,p.img_list,p.video_url,p.create_time,u.head_imgurl,u.nick_name,p.is_system";
        $list = $this->lists($postM, $postWhere, 'p.create_time desc', $field);


        $info['post_list'] = [];
        if ($list) {
            foreach ($list as &$val) {
                if ($val['img_list']) {
                    $img_list = explode(',', $val['img_list']);
                    $val['img_url'] = $img_list[0];
                } else {
                    $val['img_url'] = '';
                }
                $val['comment_num'] = M('PostComment')->where(['post_id' => $val['post_id']])->count();
            }
            unset($val);

            $info['post_list'] = $list;
        }

        $this->assign('_list', $list);
        $this->assign('meta_title', '平台帖子');
        $this->display();
    }

    public function add() {
        $id = I('id', 0, 'intval');
        if (IS_POST) {

            $data['title'] = I('title');
            $data['is_system'] = 1;
            $img = I('image_list');

            $data['img_list'] = implode(',', $img);

            if (!empty($data['img_list'])) {
                $data['post_type'] = 2;
            }

            if (empty($data['title'])) {
                $this->error('请输入标题!');
                exit;
            }

            
            if (mb_strlen($data['title'], 'UTF-8') > 12) {
                $data['title'] = mb_substr($data['title'], 0, 12, 'utf-8');
            }

            $data['content'] = I("content");

            if (mb_strlen($data['content'], 'UTF-8') > 300) {

                $data['content'] = mb_substr($data['content'], 0, 300, 'UTF-8');
            }



            if ($id == 0) {
                $res = M('Post')->add($data);
            } else {
                $res = M('Post')->where(array('post_id' => $id))->save($data);
            }

            $this->success('操作成功', U('systemPost'));

//            if ($res) {
//                $this->success('操作成功', U('index'));
//            } else {
//                $this->error('操作失败');
//            }
        } else {
            $data = M('Post')->where(['post_id' => $id])->find();

            if ($data) {
                $data['image_list'] = explode(',', $data['img_list']);
            }

            $this->assign('data', $data);
            $this->display();
        }
    }

    public function delCom() {
        $id = I('id', 0, 'intval');

        if ($id == 0) {
            $this->error('请确认要删除的评论');
        }

        $res = M('PostComment')->where(['comment_id' => $id])->delete();

        if ($res) {
            $this->success('删除成功');
        } else {
            $this->error('删除失败');
        }
    }

}
